package hadoop.hdfs.demo;

import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import org.apache.hadoop.io.DoubleWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.io.WritableComparable;

import java.io.DataInput;
import java.io.DataOutput;
import java.io.IOException;

/**
 * @author qiss
 * @create 2018-02-12 18:18
 **/
@Data
@NoArgsConstructor
public class OrderBean implements WritableComparable<OrderBean> {
    private Text itemid;
    private DoubleWritable amount;

    public OrderBean(Text itemid,DoubleWritable amount){
        set(itemid,amount);
    }

    private void set(Text itemid, DoubleWritable amount) {
        this.itemid = itemid;
        this.amount= amount;
    }


    public int compareTo(OrderBean o) {
        int i = this.itemid.compareTo(o.getItemid());
        if(i==0){
            i = -this.amount.compareTo(o.getAmount());
        }
        return i;
    }

    public void write(DataOutput out) throws IOException {
        out.writeUTF(itemid.toString());
        out.writeDouble(amount.get());

    }

    public void readFields(DataInput in) throws IOException {
        String readUTF = in.readUTF();
        double readDouble = in.readDouble();

        this.itemid = new Text(readUTF);
        this.amount= new DoubleWritable(readDouble);

    }
    @Override
    public String toString(){
        return itemid.toString() + "\t" + amount.get();
    }

}
